package com.android.pdd.baseapp.base;


import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.android.pdd.baseapp.R;
import com.android.pdd.baseapp.adapter.ClassItemListAdapter;
import com.blankj.utilcode.util.ToastUtils;
import com.lg.meng.model.ClassItem;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseClassItemListFragment extends BaseFragment {
    TextView titleTv;
    RecyclerView recyclerView;
    private ClassItemListAdapter listAdapter;

    private List<ClassItem> classItemList = new ArrayList<>();


    @Override
    protected int provideContentViewId() {
        return R.layout.lib_layout_fragment_class_item_list;
    }

    @Override
    protected void initViews() {
        recyclerView = findViewById(R.id.recyclerView);
        initClassItemList();
        listAdapter = new ClassItemListAdapter(classItemList);
        recyclerView.setAdapter(listAdapter);

        listAdapter.setOnItemClickListener((adapter, view, position) -> {
            ClassItem item = listAdapter.getItem(position);
            if (!interceptItem(item)) {
                Class itemClz = item.getClz();
                if (itemClz != null) {
                    jumpTo(itemClz);
                } else {
                    ToastUtils.showLong("item没有被拦截，itemClz设置为空");
                }
            }
        });

        titleTv = findViewById(R.id.tv_title);
        String title = provideTitle();
        titleTv.setText(title == null ? "未设置标题" : title);
    }

    @Override
    protected void initData() {

    }

    protected abstract void initClassItemList();

    protected abstract boolean interceptItem(ClassItem item);

    protected abstract String provideTitle();

    protected void addClassItem(ClassItem classItem) {
        classItemList.add(classItem);
    }
}
